﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbpDemo.WebApi;
using Microsoft.AspNetCore.Builder;
using Swashbuckle.AspNetCore.Swagger;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
    public static class SwaggerConfigurationBuilder
    {

        public static void AddSwaggerEx(this IServiceCollection services, IConfigurationRoot configuration, Dictionary<string, string[]> apiDocsConfigs)
        {
            // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
            services.AddSwaggerGen(options =>
            {
                // Define the BearerAuth scheme that's in use
                var apikeySch = new ApiKeyScheme()
                {
                    Description =
                        "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    In = "header",
                    Type = "apiKey"
                };
                //
                // Define the BearerAuth scheme that's in use
                options.AddSecurityDefinition("bearerAuth", apikeySch);
                // Assign scope requirements to operations based on AuthorizeAttribute
                options.OperationFilter<SecurityRequirementsOperationFilter>();
               foreach (var cfg in apiDocsConfigs)
                {
                    foreach (var name in cfg.Value)
                    {
                        options.IncludeXmlComments($"{AppDomain.CurrentDomain.BaseDirectory}\\{name}.xml");
                    }
                }
            });

            foreach (var cfg in apiDocsConfigs)
            {
                services.AddSwaggerGen(options =>
                {
                    options.SwaggerDoc(cfg.Key, new Info { Title = $"{cfg.Key} api", Version = "v1" });
                    options.DocInclusionPredicate((docName, apiDesc) =>
                    {
                        var moduleName = ((System.Reflection.MemberInfo)
                                ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)apiDesc.ActionDescriptor)
                                .ControllerTypeInfo).Module.Name;
                        moduleName = moduleName.Replace(".dll", "");
                        return apiDocsConfigs.First(it => it.Key == docName).Value.Contains(moduleName);
                    });
                });
            }
        }

        public static void UseSwaggerEx(this IApplicationBuilder app, Dictionary<string, string[]> apiDocsConfigs)
        {
            // Enable middleware to serve generated Swagger as a JSON endpoint
            app.UseSwagger();
            // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
            app.UseSwaggerUI(options =>
            {
                //options.InjectOnCompleteJavaScript("/swagger/ui/abp.js");
                //options.InjectOnCompleteJavaScript("/swagger/ui/on-complete.js");
                foreach (var doc in apiDocsConfigs)
                {
                    options.SwaggerEndpoint($"/swagger/{doc.Key}/swagger.json", $"{doc.Key} API");
                }
            }); // URL: /swagger
        }
    }
}
